// pages/shopping-list/shopping-list.js
import get from '../../util/get'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shoppings:[],
        shoppingss:[],
        key:[],
        color:['#FA8924','#707070','#707070'],
        arrow:['↑'],
        brand:[],
        display:['none'],
        aaaa:false
    },
    back(){
        wx.navigateBack({
          delta: 1,
        })
    },
    search(){
        this.setData({
            shoppings:this.data.shoppingss
        })
        if(this.data.key == null){
            return
        }
        let shopping = []
        for(let i=0;i<this.data.shoppings.length;i++){
            if(this.data.shoppings[i].productName.indexOf(this.data.key)!=-1){
                shopping.push(this.data.shoppings[i])
            }
        }
        if(shopping.length == 0){
            wx.showToast({
                title: '没有找到该商品',
                icon:'none'
            })
            return
        }
        this.setData({
            shoppings:shopping
        })
    },
    getinput(e){
       this.setData({
           key:e.detail.value
       })
    },
   async getshopping(){
       let shopping = await get('/products');
       this.setData({
           shoppings:shopping.rows,
           shoppingss:shopping.rows
       })
    },
    godetails(e){
        let name 
        let img
        let salePrice
        for(let i=0;i<this.data.shoppings.length;i++){
            if(e.currentTarget.dataset.id == this.data.shoppings[i].productId){
                name = this.data.shoppings[i].productName
                img = this.data.shoppings[i].productImgs
                salePrice = this.data.shoppings[i].salePrice
            }
        }
        wx.navigateTo({
          url: `/pages/shopping-details/shopping-details?name=${name}&img=${img}&salePrice=${salePrice}`,
        })
    },
    async sort(e){
        if(e.currentTarget.dataset.id == 0){
            this.setData({
                color:['#FA8924','#707070','#707070']
            })
            let shopping = await get('/products?sorted=0');
            this.setData({
                shoppings:shopping.rows,
                shoppingss:shopping.rows
            })
        }
        if(e.currentTarget.dataset.id == 1){
            this.setData({
                color:['#707070','#FA8924','#707070']
            })
            let shopping = await get('/products?sorted=1');
            this.setData({
                shoppings:shopping.rows,
                shoppingss:shopping.rows
            })
        }
        if(e.currentTarget.dataset.id == 2){
            if(this.data.color[2] == '#FA8924'&&this.data.arrow == '↑'){
                let shopping = await get('/products?sorted=3')
                this.setData({
                    shoppings:shopping.rows,
                    shoppingss:shopping.rows
                })
                this.setData({
                    arrow:'↓'
                })
                return
            }
            this.setData({
                color:['#707070','#707070','#FA8924']
            })
            let shopping = await get('/products?sorted=2')
            this.setData({
                shoppings:shopping.rows,
                shoppingss:shopping.rows,
                arrow:'↑'
            })
        }
        if(e.currentTarget.dataset.id == 3){
            this.setData({
                color:['#707070','#707070','#707070','#FA8924']
            })
        }
    },
    async brand(){
        let brands = await get('/brands')
        this.setData({
            brand:brands
        })
    },
    brandBox(){
        if(this.data.display == 'none'){
            this.setData({
                display:'block'
            })
        }
        else{
            this.setData({
                display:'none'
            })
        }
    },
    pitch(e){
        let brands = this.data.brand
        for(let i=0;i<this.data.brand.length;i++){
            if(e.currentTarget.dataset.id == this.data.brand[i]._id){
                if(this.data.brand[i].sort == 'css'){
                    brands[i].sort = null
                }
                else{
                    brands[i].sort = 'css'
                }
            }
        }
        this.setData({
          brand:brands          
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getshopping()
        this.brand()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})